package com.zhuss.product.processor;

import com.zhuss.bean.bo.product.train.SellTrainBo;
import com.zhuss.bean.vo.product.train.DetailTrainVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.*;
import com.zhuss.exception.DzcxException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.product.db.dao.ext.LineStrategyDao;
import com.zhuss.product.db.dao.ext.TrainDao;
import com.zhuss.product.db.entity.LineStrategy;
import com.zhuss.product.db.entity.Train;
import com.zhuss.product.processor.cache.LineStrategyCache;
import com.zhuss.product.processor.cache.SiteStrategyCache;
import com.zhuss.product.processor.cache.StrategyTrainCache;
import com.zhuss.product.processor.cache.TrainCache;
import com.zhuss.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author summer
 * @date 2021/1/13 18:00
 */

@Component("trainSell")
public class TrainSell extends AbstractProcessor<Object, SellTrainBo> implements Processor<Object, SellTrainBo> {

    @Autowired
    private TrainDao trainDao;

    @Autowired
    private LineStrategyDao lineStrategyDao;

    @Autowired
    @Qualifier("trainRead")
    private TrainRead trainRead;

    @Autowired
    @Qualifier("trainCache")
    private TrainCache trainCache;

    @Autowired
    @Qualifier("strategyTrainCache")
    private StrategyTrainCache strategyTrainCache;

    @Autowired
    @Qualifier("lineStrategyCache")
    private LineStrategyCache lineStrategyCache;

    @Autowired
    @Qualifier("siteStrategyCache")
    private SiteStrategyCache siteStrategyCache;

    @Override
    public void check(SellTrainBo sellTrainBo) throws DzcxException {
        if(sellTrainBo.getTrainId() == null || sellTrainBo.getTrainId() <=0l) {
            throw new DzcxException(ExceptionCode.车次id为空);
        }

        Train train = trainDao.selectByPrimaryKey(sellTrainBo.getTrainId());
        if (train.getCanSell().equals(sellTrainBo.getCanSell())) {
            throw new DzcxException(ExceptionCode.此数据不允许当前操作);
        }

        //判断策略表的数据canSell状态是否打开
        LineStrategy strategy = lineStrategyDao.selectByPrimaryKey(train.getLineStrategyId());
        if (sellTrainBo.getCanSell().equals(CanSellType.售卖.key())) {
            if (strategy.getCanSell().equals(CanSellType.售停.key())) {
                throw new DzcxException(ExceptionCode.请到车次维护进行售卖操作);
            }
        }
    }

    @Override
    public Object process(SellTrainBo bo) throws DzcxException {
        Long trainId = bo.getTrainId();

        Train record = new Train();
        record.setTrainId(trainId);
        record.setCanSell(bo.getCanSell());

        record.setModifierId(bo.getConcurrentUserId());
        record.setModifier(bo.getConcurrentUserName());
        record.setModifyTime(new Date());
        trainDao.updateByPrimaryKeySelective(record);

        DetailTrainVo vo = trainRead.detail(bo.getTrainId());
        strategyTrainCache.clear(vo.getLineStrategyId());

        trainCache.clear(trainId);
        //更新canSell状态(如果改策略下所有的train都是售停状态，策略表更新成售停状态)
        if (trainDao.selectTrainSellByLineStrategyId(trainId,CanSellType.售卖.key(),vo.getLineStrategyId()) ==0 ) {
            LineStrategy strategy = new LineStrategy();
            strategy.setLineStrategyId(vo.getLineStrategyId());
            strategy.setCanSell(IsEnum.否.key());
            lineStrategyDao.updateByPrimaryKeySelective(strategy);
            //清除线路策略缓存
            lineStrategyCache.clear(strategy.getLineStrategyId());
            siteStrategyCache.clealAll();
        }
        return null;
    }


    @Override
    public Object log(SellTrainBo bo) throws DzcxException {
        OpLog record = new OpLog();
        record.setMoudle(MoudleEnum.商品.key());
        record.setAction(LogActionEnum.车次售卖.key());
        record.setTargetId(bo.getTrainId());
        record.setTargetName(String.valueOf(bo.getTrainId()));
        record.setContent("参数: " + JsonUtil.toJson(bo));
        record.setReason(bo.getExceptionReason());

        record.setCreatorId(bo.getConcurrentUserId());
        record.setCreator(bo.getConcurrentUserName());
        record.setCreateTime(new Date());
        log(record);
        return null;
    }
}
